package com.koron.standard.jobStock.overhaul.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koron.standard.jobStock.overhaul.bean.dto.OverhaulItemDTO;
import com.koron.standard.jobStock.overhaul.bean.po.OverhaulItem;
import com.koron.standard.jobStock.overhaul.bean.po.OverhaulItemParams;
import com.koron.standard.jobStock.overhaul.mapper.OverhaulItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 大修标准下工作项目(OverhaulItem)表服务实现类
 *
 * @author jzp
 * @since 2021-09-16
 */
@Service
public class OverhaulItemService extends ServiceImpl<OverhaulItemMapper, OverhaulItem> {

    @Autowired
    private OverhaulItemParamsService overhaulItemParamsService;

    public List<OverhaulItemDTO> getDtoByOverhaulId(String overhaulId) {
        return getBaseMapper().selectDtoListByOverhaulId(overhaulId);
    }

    public List<OverhaulItemDTO> getTreeByOverhaulId(String overhaulId) {
        return toTree(getDtoByOverhaulId(overhaulId));
    }

    /**
     * DTO > tree
     */
    public List<OverhaulItemDTO> toTree(List<OverhaulItemDTO> itemDtos) {
        List<OverhaulItemDTO> itemDtoRoots = itemDtos.stream().filter(dto -> "0".equals(dto.getParentId())).collect(Collectors.toList());
        for (OverhaulItemDTO itemDtoRoot : itemDtoRoots) {
            List<OverhaulItemDTO> children = new ArrayList<>();
            for (int i = 0; i < itemDtos.size(); i++) {
                if (itemDtoRoot.getId().equals(itemDtos.get(i).getParentId())) {
                    children.add(itemDtos.get(i));
                    itemDtos.remove(i);
                    i = i - 1;
                }
            }
            itemDtoRoot.setChildren(children);
        }
        return itemDtoRoots;
    }

    public void saveRootDto(OverhaulItemDTO overhaulItemDTO) {
        saveDto(overhaulItemDTO, "0");
    }

    public void saveDto(OverhaulItemDTO overhaulItemDTO, String parentId) {

        // 1. 保存工作项目
        overhaulItemDTO.setParentId(parentId);
        save(overhaulItemDTO);

        String overhaulId = overhaulItemDTO.getOverhaulId();
        String overhaulItemId = overhaulItemDTO.getId();

        // 2. 保存记录项
        List<OverhaulItemParams> overhaulItemParams = overhaulItemDTO.getItemParams();
        // 设置作业标准id和作业标准项id
        for (OverhaulItemParams overhaulItemParam : overhaulItemParams) {
            overhaulItemParam.setOverhaulId(overhaulId);
            overhaulItemParam.setItemId(overhaulItemId);
        }
        overhaulItemParamsService.saveBatch(overhaulItemParams);
        List<OverhaulItemDTO> overhaulItemDTOS = Optional.ofNullable(overhaulItemDTO.getChildren()).orElse(new ArrayList<>());

        // 3. 保存子节点工作项目
        for (OverhaulItemDTO child : overhaulItemDTOS) {
            child.setOverhaulId(overhaulId);
            saveDto(child, overhaulItemId);
        }

    }

    public void removeByOverhaulId(String overhaulId) {
        if (StrUtil.isEmpty(overhaulId)) {
            return;
        }
        LambdaQueryWrapper<OverhaulItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OverhaulItem::getOverhaulId, overhaulId);
        List<OverhaulItem> list = list(wrapper);
        removeByIds(list.stream().map(OverhaulItem::getId).collect(Collectors.toList()));
        overhaulItemParamsService.removeByOverhaulId(overhaulId);
    }
}
